//我的变量 snowbear
var isOpen = "";// 标识是否有方案打开，在本界面中因为不考虑和其他界面的联系，因此改值在初始化
var deptId;
var schemeName = "";
var schemeType;

var scheduleUid = "${request.getParameter('SCHEDULEUID')}";// 标识正在选中的方案的scheduleUid
var schemeUid = "";// 标识已经打开的方案scheduleUid，未打开就是空的

// 的时候应该是未打开的状态，然后再打开方案之后，需要将这个值设置为true
// 这样做还是有问题，因为当界面刷新之后，这个变量的值是保存不下来的，所以只能进行一次操作
// 也就是说：isOpen这个变量的值不应该随着界面刷新变化，应该是从后台中取出来的值


var url;
var tabNum;
// 打开调度方案，需要根据树的当前选项来设置参数
// 那么是根据树的当前节点还是根据dataset的当前记录来获取到该值？
// 点击onclick事件时，确定了打开方案的scheduleUid和deptid


//初始化变量值，主要是获取当前页面的方案打开情况，还有打开的方案是个什么情况
//@Bind #view.onReady
!function(ajaxactionInit){
	ajaxactionInit.execute();
}

//@Bind #ajaxactionInit.onSuccess
!function(ajaxactionInit){
	var entity = ajaxactionInit.get("returnValue");
	isOpen = entity.isOpen;
	schemeType = entity.schemeType;
	deptId = entity.deptId;
	schemeName = entity.schemeName;
	if(isOpen == true&&schemeName != null){
		view.get("#labelScheme").set("text", "当前方案：" + schemeName);
		schemeUid = entity.schemeUid;
	}else if(isOpen == false){
		view.get("#labelScheme").set("text", "当前方案：无");
	}
	//dorado.MessageBox.alert(isOpen);
}


//同步计划 执行updateaction
//@Bind #menuitemStartSync.onClick
!function(command_syncScheme) {
	command_syncScheme.execute();
}	
//如果执行成功，那么就展示subwindow
//@Bind #command_syncScheme.onSuccess
!function(subwindowSynInfo,datasetSyncInfo){
	subwindowSynInfo.show();
	record = datasetSyncInfo.getData("#");
	//如果当前记录是空的，那么就插入一条新的纪录
	if (!record) {
		datasetSyncInfo.insert();
	}
	record.set("SyncInfo", command_syncScheme.outParameters().getValue("SyncInfo"));
	refreshMainTab();
}

//导入物料信息
//@Bind #syncPartFromMid.onClick
!function(commandSyncPartFromMid){
	commandSyncPartFromMid.execute();
}

//导入设备信息
//@Bind #syncResourceFromMid.onClick
!function(commandSyncResourceFromMid){
	commandSyncResourceFromMid.execute();
}

//导入工艺信息
//@Bind #syncRoutineFromMid.onClick
!function(commandSyncRoutineFromMid){
	commandSyncRoutineFromMid.execute();
}

//导入需求指令
//@Bind #syncDemandFromMid.onClick
!function(commandSyncDemandFromMid){
	commandSyncDemandFromMid.execute();
	refreshMainTab();
}

//导入在制任务
//@Bind #syncTaskFromMid.onClick
!function(commandSyncTaskFromMid){
	commandSyncTaskFromMid.execute();
	refreshMainTab();
}

//计划优化，默认为粒子群算法，迭代30代，种群大小30
//@Bind #menuitemZje.onClick
!function(commandScheduleByZje){
	if(confirm("计划优化耗时较长，是否继续？"))
		commandScheduleByZje.execute();
}

//遗传算法
//@Bind #menuGA1.onClick
!function(subwindowGAParameter){
	subwindowGAParameter.show();
}

//开始计算遗传算法
//@Bind #buttonBeginGA.onClick
!function(datasetParameter,commandGA,commandGAAutomation,datasetSchedule){
	var record = datasetParameter.getData("#");
	var size=record.get("size");
	var loop= record.get("loop");
	var crossoverPro= record.get("crossoverPro");
	var mutationPro= record.get("mutationPro");
	var PSGA= record.get("PSGA");

	if(size<=0 || loop<=0||size==null){
	  alert("种群大小和迭代次数必须大于0！");
	  return;
	}
	if(crossoverPro>1 || crossoverPro<0 || mutationPro>1 || mutationPro<0||mutationPro==null||crossoverPro==null){
	  alert("交叉概率和变异概率必须在0~1之间！");
	  return;
	}
	if(size<20){
	  alert("种群数量过小，求解效果可能不明显！");  
	}
	if(loop<20){
	  alert("迭代次数过少，求解效果可能不明显！");
	}
	commandGA.set("parameter",$map({
		size : size,
		loop : loop
	}));
	//测试代码
	/*record.set("GALeftTime",1+"分钟");
	datasetParameter.flushAsync();
	datasetParameter.insert(record);*/
	commandGAAutomation.set("parameter",$map({
		size : size,
		loop : loop,
		crossoverPro : crossoverPro,
		mutationPro : mutationPro,
		PSGA : PSGA
	}));
	commandGA.execute();
}

//@Bind #commandGA.onSuccess
!function(commandGA,datasetParameter,commandGAAutomation){
	var record = datasetParameter.getData("#");
	var map = commandGA.get("returnValue");
	var oneTime= map.oneTime;
	var size=commandGA.get("parameter").get("size");
	var loop=commandGA.get("parameter").get("loop");
	record.set("GALeftTime",(Math.round(oneTime*size*loop/60*10))/10+"分钟");
	datasetParameter.flushAsync();
	datasetParameter.insert(record);
	commandGAAutomation.execute();
}

//@Bind #commandGAAutomation.onSuccess
!function(commandGAAutomation){
	var map=commandGAAutomation.get("returnValue");
	var allTime = map.allTime;
	allTime=(Math.round(allTime/60*10))/10+"分钟";
	alert("计算结束！\n实际计算时间："+allTime);
	subwindowGAParameter.hide();
	refreshMainTab();
}

//粒子群算法，基于优先级编码的粒子群算法
//@Bind #menuitemPSOPara.onClick
!function(){
	subwindowPSOParameter.show();
}

//保存计划
//@Bind #menuitemSaveScheme.onClick
!function(command_saveScheme){
	command_saveScheme.execute()
}

//如果保存计划成功
//@Bind #command_saveScheme.onSuccess
!function(){
	window.status = "\u8c03\u5ea6\u65b9\u6848\u4fdd\u5b58\u6210\u529f\uff01";
	alert("\u8c03\u5ea6\u65b9\u6848\u4fdd\u5b58\u6210\u529f\uff01\n");
	refreshMainTab();
}

//如果保存计划失败
//@Bind #command_saveScheme.onFailure
!function(){
	window.status = "\u8c03\u5ea6\u65b9\u6848\u4fdd\u5b58\u5931\u8d25\uff01\uff01\uff01\uff01";
	//alert("调度方案保存失败！！！！！");
}

//输出中间方案
//@Bind #menuitemExportScheme.onClick
!function(subwindowExportSchemeName,datasetExportScheme){
	subwindowExportSchemeName.show();
	datasetExportScheme.insert();
}


// @Bind #menuitemOpenScheme.onClick
!function(ajaxactionOpenScheme, treeSchemeManage, command_closeScheme,
		schemeFrame, view,treeOrder,treeResource) {
	if (isOpen == true) {// 如果是已经打开的状态，需要将已经打开的方案关闭
		command_closeScheme.set("parameter", $map({
			scheduleUid : schemeUid
		}));
		command_closeScheme.execute();// 没有设置关闭的谁的schemeUid
		isOpen = false;
	}
	// 关闭方案之后再重新打开方案
	var entity = treeSchemeManage.get("currentEntity");
	schemeUid = entity.get("scheduleUid");
	var schemeName = entity.get("scheduleName");
	deptId = entity.get("deptUid");
	var url = "http://localhost:" + window.location.port
			+ "/mm-dorado/view.schedule.scheduleSchemeManage.d?SCHEDULEUID"
			+ schemeUid + "&deptId=" + deptId;
	// schemeFrame.set("path", url)
	// dorado.MessageBox.alert(schemeUid + ":" + deptId);
	ajaxactionOpenScheme.set("parameter", $map({
		scheduleUid : schemeUid
	}));
	ajaxactionOpenScheme.execute();
	isOpen = true;
	view.get("#labelScheme").set("text", "当前方案：" + schemeName);
	treeResource.refresh(true);
	treeOrder.refresh(true);
}

// @Bind #menuitemCloseScheme.onClick
!function(command_closeScheme,view) {
	if (confirm("您确定要关闭计划吗？请注意先保存计划")) {
		command_closeScheme.set("parameter", $map({
			scheduleUid : schemeUid
		}));
		command_closeScheme.execute()
		isOpen = false;
		schemeUid = "";
	}
	view.get("#treeResource").refresh(true);
	view.get("#treeOrder").refresh(true);
}

//新建方案
//@Bind #menuitemNewScheme.onClick
!function(iframeCreate,dialogCreate,treeSchemeManage){
	dorado.MessageBox.alert(schemeType);
	var url = "http://localhost:" + window.location.port +
			"/mm-dorado/view.schedule.scheduleSchemeNew.d?SCHEDULEUID=" + scheduleUid + "&schemeType=" + schemeType;
	iframeCreate.set("path",url);
	dialogCreate.show();
}

//@Bind #dialogCreate.onHide
!function(datasetCreatedScheme){
	datasetCreatedScheme.flushAsync();
}

/**
 * 所有的tab 路径
 * 
 * @param view
 * @returns
 */
// 委外任务 checked
// @Bind #tabCoTaskOut.onClick
!function(view,treeOrder) {
	//获取树的当前节点，然后取出相应的数据
	var entity = treeOrder.get("currentEntity");
	var orderNum = entity.get("orderNum");
	url = "http://localhost:"
			+ window.location.port
			+ "/mm-dorado/view.schedule.cooperationTaskOut.d?SCHEDULEUID="+scheduleUid+"&orderNum="+orderNum;
	//dorado.MessageBox.alert(url);
	view.get("#mainFrame").set("path", url);
	view.get("#mainFrame").reload();
}

// 协作任务 
// @Bind #tabCoTaskIn.onClick
!function(view,treeOrder) {
	//获取树的当前节点，然后取出相应的数据
	var entity = treeOrder.get("currentEntity");
	var orderNum = entity.get("orderNum");
	url = "http://localhost:"
			+ window.location.port
			+ "/mm-dorado/view.schedule.cooperationTaskIn.d?SCHEDULEUID="+scheduleUid+"&orderNum="+orderNum;
	view.get("#mainFrame").set("path", url);
	view.get("#mainFrame").reload();
}

// 交付订单信息
// @Bind #tabFinishedOrder1.onClick
!function(view,treeOrder) {
	//获取树的当前节点，然后取出相应的数据
	var entity = treeOrder.get("currentEntity");
	var orderNum = entity.get("orderNum");
	url = "http://localhost:"
			+ window.location.port
			+ "/mm-dorado/view.schedule.scheduleSectionFinishedOrder.d?SCHEDULEUID="+scheduleUid+"&deptId="+deptId+"&schemeType="+schemeType+"&orderNum="+orderNum+"&demandUid=&schemeUid="+schemeUid;
	view.get("#mainFrame").set("path", url);
	view.get("#mainFrame").reload();
}

// 已完零件信息
// @Bind #tabFinishedPartTask1.onClick
!function(view,treeOrder) {
	var entity = treeOrder.get("currentEntity");
	var orderNum = entity.get("orderNum");
	url = "http://localhost:"
			+ window.location.port
			+ "/mm-dorado/view.schedule.scheduleFinishedPartTask.d?SCHEDULEUID="+scheduleUid+"&deptId="+deptId+"&schemeType="+schemeType+"&orderNum="+orderNum+"&demandUid=&schemeUid="+schemeUid;
	view.get("#mainFrame").set("path", url);
	view.get("#mainFrame").reload();
}

// 方案管理
// @Bind #schemeFrame.onClick
!function(view) {
	url = "http://localhost:"
			+ window.location.port
			+ "/mm-dorado/view.schedule.scheduleSchemeManage.d?SCHEDULEUID="+scheduleUid+"&DEPTID="+deptId;
	view.get("#mainFrame").set("path", url);
	view.get("#mainFrame").reload();
}

// 参数维护
// @Bind #parameterFrame.onClick
!function(view,treeSchemeManage) {
	var entity = treeSchemeManage.get("currentEntity");
	var tempScheduleUid = entity.get("scheduleUid");
	url = "http://localhost:" + window.location.port
			+ "/mm-dorado/view.schedule.scheduleParaNew.d?SCHEDULEUID="+tempScheduleUid;
	view.get("#mainFrame").set("path", url);
	view.get("#mainFrame").reload();
}

// 工作中心任务☆
// @Bind #tabWorkCenterTask.onClick
!function(view,treeResource) {
	var entity = treeResource.get("currentEntity");
	var workcenterUid = entity.get("workCenterUid");
	var workcenterName = entity.get("workCenterName");
	url = "http://localhost:"
			+ window.location.port
			+ "/mm-dorado/view.schedule.ScheduleWorkCenterTask.d?SCHEDULEUID="+scheduleUid+"&WORKCENTERUID="+workcenterUid+"&WORKCENTERNAME="+workcenterName;
	//dorado.MessageBox.alert(url);
	view.get("#mainFrame").set("path", url);
	view.get("#mainFrame").reload();
}

// 工作中心产能评估
// @Bind #tabDeptInfoWorkCenterLoad.onClick
!function(view,treeResource) {
	var entity = treeResource.get("currentEntity");
	var workcenterUid = entity.get("workCenterUid");
	var workcenterName = entity.get("workCenterName");
	url = "http://localhost:"
			+ window.location.port
			+ "/mm-dorado/view.schedule.ScheduleWorkCenterTask.d?SCHEDULEUID="+scheduleUid+"&WORKCENTERUID="+workcenterUid+"&WORKCENTERNAME="+workcenterName;
	view.get("#mainFrame").set("path", url);
	view.get("#mainFrame").reload();
}

// 订单预警
// @Bind #tabOrderWarning.onClick
!function(view,treeOrder) {
	
	//获取树的当前节点，然后取出相应的数据
	var entity = treeOrder.get("currentEntity");
	var orderNum = entity.get("orderNum");
	url = "http://localhost:"
			+ window.location.port
			+ "/mm-dorado/view.schedule.ScheduleSectionOrderWarning.d?SCHEDULEUID="+scheduleUid+"&deptId="+deptId+"&DEVICEUID=a1&DEVICENAME=%E4%B8%89%E5%9D%90%E6%A0%871";
	view.get("#mainFrame").set("path", url);
	view.get("#mainFrame").reload();
}

// 订单管理
// @Bind #tabOder.onClick
!function(view,treeOrder) {	
	//获取树的当前节点，然后取出相应的数据
	var entity = treeOrder.get("currentEntity");
	var orderNum = entity.get("orderNum");
	url = "http://localhost:"
			+ window.location.port
			+ "/mm-dorado/view.schedule.scheduleSectionOrderForSummaryPlan.d?SCHEDULEUID="+scheduleUid+"&deptId="+deptId+"&schemeType="+schemeType+"&orderNum="+orderNum+"&demandUid=&schemeUid="+schemeUid;
	view.get("#mainFrame").set("path", url);
	view.get("#mainFrame").reload();
}

// 班产任务
//@Bind #tabShiftProduction.onClick
!function(view,treeResource) {
	//获取树的当前节点，然后取出相应的数据
	var entity = treeResource.get("currentEntity");
	var deviceUid = entity.get("deviceUid");
	var deviceName = entity.get("deviceName");
	url = "http://localhost:"
			+ window.location.port
			+ "/mm-dorado/view.schedule.taskbc.d?SCHEDULEUID="+scheduleUid+"&DEVICEUID="+deviceUid+"&DEVICENAME="+deviceName;
	view.get("#mainFrame").set("path", url);
	view.get("#mainFrame").reload();
}

// 设备能力
//@Bind #tabDeviceCapacity.onClick
!function(view,treeResource) {
	//获取树的当前节点，然后取出相应的数据
	var entity = treeResource.get("currentEntity");
	var deviceUid = entity.get("deviceUid");
	var deviceName = entity.get("deviceName");	
	url = "http://localhost:"
			+ window.location.port
			+ "/mm-dorado/view.schedule.scheduleDeviceCapacity.d?SCHEDULEUID="+scheduleUid+"&DEVICEUID="+deviceUid+"&DEVICENAME="+deviceName;
	view.get("#mainFrame").set("path", url);
	view.get("#mainFrame").reload();
}

// 瓶颈分析
// @Bind #tabBottleNeckAnalysis.onClick
!function(view,treeResource) {
	//获取树的当前节点，然后取出相应的数据
	var entity = treeResource.get("currentEntity");
	var deptUid = entity.get("deptUid");
	url = "http://localhost:"
			+ window.location.port
			+ "/mm-dorado/view.schedule.bottleNeckAnalysis.d?SCHEDULEUID="+scheduleUid+"&deptUid="+deptUid+"&schemeType="+schemeType;
	view.get("#mainFrame").set("path", url);
	view.get("#mainFrame").reload();
}

// 基本日历
// @Bind #tabBaseCalender.onClick
!function(view) {
	url = "http://localhost:" + window.location.port
			+ "/mm-dorado/view.schedule.baseCalendar.d?SCHEDULEUID="+scheduleUid;
	view.get("#mainFrame").set("path", url);
	view.get("#mainFrame").reload();
}

// 已分配任务
// @Bind #tabTaskAssign.onClick
!function(view,treeResource) {
	//获取树的当前节点，然后取出相应的数据
	var entity = treeResource.get("currentEntity");
	var deviceUid = entity.get("deviceUid");
	var deviceName = entity.get("deviceName");	
	url = "http://localhost:"
			+ window.location.port
			+ "/mm-dorado/view.schedule.DeviceTaskAssign.d?SCHEDULEUID="+scheduleUid+"&DEVICEUID="+deviceUid+"&DEVICENAME="+deviceName;
	view.get("#mainFrame").set("path", url);
	view.get("#mainFrame").reload();
}

// 零件甘特图
// @Bind #tabSchedulePartGantt.onClick
!function(view) {
	url = "http://localhost:" + window.location.port
			+ "/mm-dorado/AllPartTaskGantt.jsp";
	view.get("#mainFrame").set("path", url);
	view.get("#mainFrame").reload();
}

//在制任务管理
//@Bind #tabSchedulePartTask.onClick
!function(view) {
	url = "http://localhost:" + window.location.port
			+ "/mm-dorado/view.schedule.schedulePartTask.d?SCHEDULEUID="+ scheduleUid+"&DEPTID="+deptId;
	view.get("#mainFrame").set("path", url);
	view.get("#mainFrame").reload();
}

function closeTabControl(view) {
	view.get("^tabset").set("visible", false);
}

// @Bind #treeSchemeManage.onClick
!function(view, tabcontrolMultiScheme) {
	closeTabControl(view);
	view.get("#tabcontrolMultiScheme").set("visible", true);
	var currentIndex = tabcontrolMultiScheme.get("currentIndex");
	var tab = tabcontrolMultiScheme.get("currentTab");
	tabcontrolMultiScheme.set("currentTab", currentIndex);
	//触发当前tab的onclick事件
	tab.fireEvent("onClick");
}

// @Bind #treeOrder.onClick
!function(treeOrder, view, tabsetOrder, tabsetOrderItem) {
	var currentNode = treeOrder.get("currentNode");
	var level = currentNode.get("level");
	if (level == "1") {
		/**
		 * 测试代码
		 */
		view.get("#tabcontrolMultiScheme").set("visible", false);
		view.get("#tabsetOrder").set("visible", true);
		var currentIndex = tabsetOrder.get("currentIndex");
		var tab = tabsetOrder.get("currentTab");
		tabsetOrder.set("currentTab", currentIndex);
		tab.fireEvent("onClick");
		/*url = "http://localhost:"
				+ window.location.port
				+ "/mm-dorado/view.schedule.cooperationTaskOut.d?SCHEDULEUID=4100274&orderNum=3";
		view.get("#mainFrame").set("path", url);
		view.get("#mainFrame").reload();*/
	} else if (level == "2") {
		closeTabControl(view);
		tabsetOrderItem.set("visible", true);
		var currentIndex = tabsetOrderItem.get("currentIndex");
		var tab = tabsetOrderItem.get("currentTab");
		tabsetOrderItem.set("currentTab", currentIndex);
		tab.fireEvent("onClick");
		/*url = "http://localhost:"
			+ window.location.port
			+ "/mm-dorado/view.schedule.ScheduleSectionOrderWarning.d?SCHEDULEUID=4100274&deptId=005243&DEVICEUID=a1&DEVICENAME=%E4%B8%89%E5%9D%90%E6%A0%871";
		view.get("#mainFrame").set("path", url);
		view.get("#mainFrame").reload();*/
	}
}

// @Bind #treeResource.onClick
!function(treeResource, view, tabsetDept, tabsetWorkCenter, tabsetDevice) {
	var currentNode = treeResource.get("currentNode");
	var level = currentNode.get("level");
	if (level == "1") {
		closeTabControl(view);
		tabsetDept.set("visible", true);
		//获取当前页面的序号
		var currentIndex = tabsetDept.get("currentIndex");
		var tab = tabsetDept.get("currentTab");
		var tempUrl = tab.get("userData");
		tabsetDept.set("currentTab", currentIndex);
		//dorado.MessageBox.alert(currentIndex);
		//dorado.MessageBox.alert(tempUrl);
		tab.fireEvent("onClick");
	} else if (level == "2") {
		closeTabControl(view);
		tabsetWorkCenter.set("visible", true);
		var tab = tabsetWorkCenter.get("currentTab");
		var currentIndex = tabsetWorkCenter.get("currentIndex");
		tabsetWorkCenter.set("currentTab", currentIndex);
		tab.fireEvent("onClick");
	} else if (level == "3") {
		closeTabControl(view);
		tabsetDevice.set("visible", true);
		var currentIndex = tabsetDevice.get("currentIndex");
		var tab = tabsetDevice.get("currentTab");
		tabsetDevice.set("currentTab", currentIndex);
		tab.fireEvent("onClick");
	}
}

//在tree展开之前应该将打开方案的scheduleUid的值传进去snow
//不对，传进去的scheduleUID应该是当前打开方案的sheduleuiD,而不是页面上的scheduleUid
//所以要先记录打开方案的scheduleUid，当前页面的参数只有一个deptId
//@Bind @datatypeDept.#workcenter.beforeLoadData
!function(){
	view.get("@datatypeDept").get("#workcenter").set("parameter",$map({
		scheduleUid : schemeUid,
		deptUid : deptId
	}));
	view.get("@datatypeDept").get("#workcenter").processDefault=true;
}

//刷新主页面，重新加载当前页面？
function refreshMainTab(){
	
}



